<?php
class EWebby_UI_Widgets_ListWidget extends EWebby_UI_Widgets_Widget
	implements EWebby_UI_Widgets_ITemplateWidget {
	const HEADERTEMPLATETAGNAME			= 'header';
	const EMPTYTEMPLATETAGNAME			= 'empty';
	const FOOTERTEMPLATETAGNAME			= 'footer';
	const GROUPTEMPLATETAGNAME 			= 'group';
	const ITEMTEMPLATETAGNAME 			= 'item';
	const GROUPSEPARATORTEMPLATETAGNAME = 'groupseparator';
	const ITEMSEPARATORTEMPLATETAGNAME 	= 'itemseparator';
	
	private $_headerText;
	private $_emptyText;
	private $_footerText;
	private $_groupText;
	private $_itemText;
	private $_itemSeparatorText;
	private $_groupSeparatorText;

	public $itemCreatedEvent;
	public $groupCreatedEvent;
	public $itemSeparatorCreatedEvent;
	public $groupSeparatorCreatedEvent;
	
	public $header;
	public $footer;
	public $empty;
	
	public function __construct() {
		parent::__construct();
		
		$this->inlineDesign = true;
		
		$this->itemCreatedEvent = new EWebby_Event();
		$this->groupCreatedEvent = new EWebby_Event();
		$this->itemSeparatorCreatedEvent = new EWebby_Event();
		$this->groupSeparatorCreatedEvent = new EWebby_Event();
	}

	protected function onLoad() {
		$nodes = $this->queryDocument('//' . self::HEADERTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->header = new EWebby_UI_Widgets_PanelWidget();
			$this->_headerText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}
		$nodes = $this->queryDocument('//' . self::EMPTYTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->empty = new EWebby_UI_Widgets_PanelWidget();
			$this->_emptyText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}
		$nodes = $this->queryDocument('//' . self::FOOTERTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->footer = new EWebby_UI_Widgets_PanelWidget();
			$this->_footerText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}

		$nodes = $this->queryDocument('//' . self::GROUPTEMPLATETAGNAME . '//' . self::ITEMTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->_itemText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
			
			$nodes[0]->parentNode->replaceChild(
    			$nodes[0]->ownerDocument->createCDATASection('<' . self::WIDGETTAG . ' name="__ITEMPANEL__" type="EWebby_ui_widgets_panelwidget"/>'),
    			$nodes[0]);
			
		}
		$nodes = $this->queryDocument('//' . self::GROUPTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->_groupText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}
		
		$nodes = $this->queryDocument('//' . self::GROUPSEPARATORTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->_groupSeparatorText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}
		$nodes = $this->queryDocument('//' . self::ITEMSEPARATORTEMPLATETAGNAME);
		if( count($nodes) > 0 ) {
			$this->_itemSeparatorText = EWebby_UI_Widgets_WidgetDocument::getInnerHTML($nodes[0]);
		}
		
		$this->_document = new EWebby_UI_Widgets_WidgetDocument($this);
	}
	
	public function setData($data) {
		if( $this->header != null ) {
			$this->addElement('header', $this->header, $this->_headerText);
		}
		
		if( $data != null ) {
			if( !($data instanceof EWebby_IEnumerable) ) {
				throw new EWebby_Exception('data must be of type EWebby_IEnumerable');
			}
			
			$startNewGroup = true;
			$index = 0;
			$groupIndex = 0;
			$newGroupWidget = null;
			
			while( $data->read() ) {
				$dataItem = $data->getCurrentItem();
				if( $startNewGroup ) {
					$index = 0;

					if( $groupIndex > 0 ) {
						$newGroupSeparatorWidget = new EWebby_UI_Widgets_PanelWidget();
						$this->addElement('groupSeparators', $newGroupSeparatorWidget, $this->_groupSeparatorText);
						$this->groupSeparatorCreatedEvent->dispatchEvent($this, 
							new EWebby_UI_Widgets_ListWidgetEventArgs($newGroupSeparatorWidget, $dataItem, $groupIndex-1) );
						
					}
					
					$newGroupWidget = new EWebby_UI_Widgets_PanelWidget();
					$this->addElement('itemgroups', $newGroupWidget, $this->_groupText);
					$this->groupCreatedEvent->dispatchEvent($this, 
						new EWebby_UI_Widgets_ListWidgetEventArgs($newGroupWidget, $dataItem, $groupIndex) );
					
					$groupIndex++;
				}
				
				if( $newGroupWidget->containsElement('__ITEMPANEL__') ) {
					$itemPanel = $newGroupWidget->elements['__ITEMPANEL__'];
					
					if( $index > 0 ) {
						$newItemSeparatorWidget = new EWebby_UI_Widgets_PanelWidget();
						$itemPanel->addElement('itemSeparators', $newItemSeparatorWidget, $this->_itemSeparatorText);
						$this->itemSeparatorCreatedEvent->dispatchEvent($this, 
							new EWebby_UI_Widgets_ListWidgetEventArgs($newItemSeparatorWidget, $dataItem, $index-1) );
					}
				
					$newItemWidget = new EWebby_UI_Widgets_PanelWidget();
					$itemPanel->addElement('items', $newItemWidget, $this->_itemText);
					
					$eventObject = new EWebby_UI_Widgets_ListWidgetItemEventArgs($newGroupWidget, $newItemWidget, $dataItem, $index, $groupIndex);
					$this->itemCreatedEvent->dispatchEvent($this, $eventObject);
					$startNewGroup = $eventObject->startNewGroup;
				}
				
				$index++;
			}
		} else {
			$this->addElement('empty', $this->empty, $this->_emptyText);
		}
		
		if( $this->footer != null ) {
			$this->addElement('footer', $this->footer, $this->_footerText);
		}
	}	
}